﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using DAL;

namespace WebAPI.Controllers
{
    public class EvtMemLogController : ApiController
    {
        private MyTeamEntities db = new MyTeamEntities();

        // GET api/EvtMemLog
        public IQueryable<EvtMemLog> GetEvtMemLogs()
        {
            return db.EvtMemLogs;
        }

        // GET api/EvtMemLog/5
        [ResponseType(typeof(EvtMemLog))]
        public IHttpActionResult GetEvtMemLog(int id)
        {
            EvtMemLog evtmemlog = db.EvtMemLogs.Find(id);
            if (evtmemlog == null)
            {
                return NotFound();
            }

            return Ok(evtmemlog);
        }

        // PUT api/EvtMemLog/5
        public IHttpActionResult PutEvtMemLog(int id, EvtMemLog evtmemlog)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != evtmemlog.log_id)
            {
                return BadRequest();
            }

            db.Entry(evtmemlog).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EvtMemLogExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/EvtMemLog
        [ResponseType(typeof(EvtMemLog))]
        public IHttpActionResult PostEvtMemLog(EvtMemLog evtmemlog)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.EvtMemLogs.Add(evtmemlog);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = evtmemlog.log_id }, evtmemlog);
        }

        // DELETE api/EvtMemLog/5
        [ResponseType(typeof(EvtMemLog))]
        public IHttpActionResult DeleteEvtMemLog(int id)
        {
            EvtMemLog evtmemlog = db.EvtMemLogs.Find(id);
            if (evtmemlog == null)
            {
                return NotFound();
            }

            db.EvtMemLogs.Remove(evtmemlog);
            db.SaveChanges();

            return Ok(evtmemlog);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool EvtMemLogExists(int id)
        {
            return db.EvtMemLogs.Count(e => e.log_id == id) > 0;
        }
    }
}